import React, { Component } from 'react'
import { Modal, Form } from 'antd';
import { ModalForm } from '../../../components'
import { fetchData } from '../../../components/AjaxConsumer'
import store from '../../../stores'
const confirm = Modal.confirm;

const WrappedAdvancedSearchForm = Form.create()(ModalForm);

export default class UpdateRolePage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            visible: false,
            resData: null,
            submitUrl: "/base/resrole/u"
        };

        this.formAlerSize = 'small'; //弹出框的大小 只有'large'和'small'两种值 （必须,输错时默认为large）
        this.getFormData = [
            {
                type : 'hiddenInput',
                name : 'resroleId',
                initialValue : 'resroleId'
            },{
                type : 'input',
                showName : '角色编码',
                name : 'resroleCode',
                initialValue : 'resroleCode',
                required: true  //选填

            },{
                type : 'input',
                showName : '角色名称',
                name : 'resroleName',
                initialValue : 'resroleName',
                required: true  //选填
            },{
                type : 'select',
                showName : '角色类型',
                name : 'resreoleType',
                optionList:[              //必填  下拉框的信息
                    {
                        optionKey:'0',
                        optionShow:'通用角色'
                    },{
                        optionKey:'1',
                        optionShow:'工厂角色'
                    }
                ],
                initialValue : 'resreoleType',
                required: true
            },{
                type : 'textarea',
                showName : '备注',
                name : 'remark',
                initialValue : 'remark'
            }
        ];
    }

    //显示弹框
    showModal = () => {
        this.setState({
            visible: true,
        });
    }
    // 隐藏弹出框
    hideModal = () => {
        this.setState({
            visible: false,
        });
    }
    addHandle = () => {
        //清空input值
        this.setState({
            submitUrl: '/base/resrole/c',
            formTitle: '新增角色',
            resData: null
        });
        this.getFormData.map( (e, index) => {
            e.initialValue = ''
        });
        this.showModal();//显示弹框
    }

    editHandle = (rowObj) => {
        this.setState({
            formTitle: '修改角色'
        });
        //根据对象的Id 属性获取对象信息
        const options= {
            url: '/base/resrole/r/' + rowObj.resroleId,
            type: 'get'
        };
        fetchData(this, options, (resData) => {
            this.setState({
                resData: resData
            });

            this.showModal();//显示弹框
        });


    }


    deleteHandle = (rowObj) => {
        const options= {
            url: '/base/resrole/d',
            condition:{
                resroleIds: rowObj.resroleId
            }
        };
        let _that = this;
        confirm({
            title: '您确定要删除吗？',
            onOk() {
                fetchData(_that, options, (resData) => {
                    _that.setState({
                        resData: resData
                    });
                    if (resData.code == '0'){
                        alert(resData.msg);
                    }else {
                        _that.refreshHandle();
                    }
                });
            },
            onCancel() {
                console.log("取消了删除操作")
            },
        });
    }
    handleOk = () => {
        this.hideModal();
        this.refreshHandle();
    }

    handleCancel = () => {
        this.hideModal();
    }

    //刷新List列表
    refreshHandle = () => {
        this.props.refreshHandle();
    }

    render() {
        if(this.state.resData != null &&  this.state.resData.data != null){
            let resRole = this.state.resData.data;
            this.getFormData.map( (e, index) => {
                e.initialValue = resRole[e.name]
            });
        }

        return(
            <Modal
                style={{top:'20px'}}
                visible={this.state.visible}
                title={this.state.formTitle ? this.state.formTitle : '填写表单'}
                onOk={this.handleOk}
                onCancel={this.handleCancel}
                width={this.formAlerSize=='large'?'850px':'400px'}
                footer=''
            >

                <WrappedAdvancedSearchForm formAlerSize={this.formAlerSize}  formUrl={this.state.submitUrl} getFormData={this.getFormData}  closeBox={this.handleCancel} checkBox={this.handleOk} />

            </Modal>
        )
    }
}
